package com.bazaaroid.mobile.android.helper;

import java.io.File;
import java.util.ArrayList;

import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class AdvancedSearchParser {
	private ArrayList<String> productNames;
	private int minPrice;
	private int maxPrice;
	private String priceRange;
	private int discount;
	private String categoryKey;
	private String partnerKey;
	private int distance;
	private String currency;
	private String locationName;
	
	public AdvancedSearchParser()
	{
		setProductNames(new ArrayList<String>(2)); // two product names only
		setPriceRange("");
		setMinPrice(setMaxPrice(-1));
		setDiscount(0);
		setCategoryKey("");
		setPartnerKey("");
		setDistance(0); // TODO: to be used in later versions
		setCurrency(""); // TODO: to be used in later versions
		setLocationName("");
	}
	
	public AdvancedSearchParser(ArrayList<String> products, String priceRange, String discount, String categoryKey, String partnerKey, String locationName)
	{
		setProductNames(products);
		this.setPriceRange(priceRange);
		setMinPrice(setMaxPrice(-1));
		extractMinMaxPrice();
		extractDiscount(discount);
		this.setCategoryKey(categoryKey);
		this.setPartnerKey(partnerKey);
		setDistance(0); // TODO: to be used in later versions
		setCurrency(""); // TODO: to be used in later versions
		setLocationName(locationName);
	}
	
	public String getAdvancedApiQueryString()
	{
		/*
			- name (product name)
			- category (category key)
			- partner (partner key)
			- location (location name)
			- priceFrom
			- priceTo
		*/
		// queryString: name=<name>&category<category_key>&partner<partner_key>&location<location_name>&priceFrom<price_from>&priceTo<priceTo>
		String queryString = "";
		boolean bParameterFound = false;
		
		if(productNames != null && productNames.size() > 0)
		{
			if(productNames.get(0) != null && !productNames.get(0).equalsIgnoreCase(""))
			{	
				queryString = String.format("%sname=%s", queryString, productNames.get(0));
				bParameterFound = true;				
			}
		}
		
		if(categoryKey != null && !categoryKey.equalsIgnoreCase("") && !categoryKey.equalsIgnoreCase("0"))
		{
			queryString = String.format("%s%scategory=%s", queryString, bParameterFound ? "&" : "", categoryKey);
			bParameterFound = true;
		}
		
		if(partnerKey != null && !partnerKey.equalsIgnoreCase("") && !partnerKey.equalsIgnoreCase("0"))
		{
			queryString = String.format("%s%spartner=%s", queryString, bParameterFound ? "&" : "", partnerKey);
			bParameterFound = true;
		}
		
		if(locationName != null && !locationName.equalsIgnoreCase(""))
		{
			queryString = String.format("%s%slocation=%s", queryString, bParameterFound ? "&" : "", locationName);
			bParameterFound = true;
		}
		
		if(minPrice > -1)
		{
			queryString = String.format("%s%spriceFrom=%s", queryString, bParameterFound ? "&" : "", String.valueOf(minPrice));
			bParameterFound = true;
		}
		
		if(maxPrice > -1 && maxPrice > minPrice)
		{
			queryString = String.format("%s%spriceTo=%s", queryString, bParameterFound ? "&" : "", String.valueOf(maxPrice));
			bParameterFound = true;
		}
		
		if(discount > 0)
		{
			queryString = String.format("%s%sdiscount=%s", queryString, bParameterFound ? "&" : "", String.valueOf(discount));
			bParameterFound = true;			
		}
		
		return queryString;
	}
	
	private void extractMinMaxPrice()
	{
		int minusSignIndex = 0;
		int plusSignIndex = 0;	
		minusSignIndex = getPriceRange().indexOf("-");
		plusSignIndex = getPriceRange().indexOf("+");
		if(minusSignIndex != -1 && minusSignIndex > 0)
		{
			setMinPrice(Integer.parseInt(getPriceRange().substring(0, minusSignIndex)));
			setMaxPrice(Integer.parseInt(getPriceRange().substring(minusSignIndex + 1, getPriceRange().length())));
		}
		if(plusSignIndex != -1 && plusSignIndex > 0)
		{
			setMinPrice(Integer.parseInt(getPriceRange().substring(0, plusSignIndex)));
			setMaxPrice(getMinPrice());
		}
	}
	
	private void extractDiscount(String discount)
	{
		int plusSignIndex = 0;
		plusSignIndex = discount.indexOf("+");
		if(plusSignIndex > -1)
		{
			this.setDiscount(Integer.parseInt(discount.substring(0, plusSignIndex)));
		}
		else
		{
			this.setDiscount(Integer.parseInt(discount));
		}
	}

	public void setProductNames(ArrayList<String> productNames) {
		this.productNames = productNames;
	}

	public ArrayList<String> getProductNames() {
		return productNames;
	}

	public void setMinPrice(int minPrice) {
		this.minPrice = minPrice;
	}

	public int getMinPrice() {
		return minPrice;
	}

	public int setMaxPrice(int maxPrice) {
		this.maxPrice = maxPrice;
		return maxPrice;
	}

	public int getMaxPrice() {
		return maxPrice;
	}

	public void setPriceRange(String priceRange) {
		this.priceRange = priceRange;
	}

	public String getPriceRange() {
		return priceRange;
	}

	public void setDiscount(int discount) {
		this.discount = discount;
	}

	public int getDiscount() {
		return discount;
	}

	public void setCategoryKey(String categoryKey) {
		this.categoryKey = categoryKey;
	}

	public String getCategoryKey() {
		return categoryKey;
	}

	public void setPartnerKey(String partnerKey) {
		this.partnerKey = partnerKey;
	}

	public String getPartnerKey() {
		return partnerKey;
	}

	public void setDistance(int distance) {
		this.distance = distance;
	}

	public int getDistance() {
		return distance;
	}

	public void setCurrency(String currency) {
		this.currency = currency;
	}

	public String getCurrency() {
		return currency;
	}

	public void setLocationName(String locationName) {
		this.locationName = locationName;
	}

	public String getLocationName() {
		return locationName;
	}
}
